// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.moc.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.MocRecord;
import com.liuxinlong.modules.moc.controller.request.ImportMocDataReq;
import com.liuxinlong.modules.moc.controller.request.QueryMocRecordReq;
import com.liuxinlong.modules.moc.controller.request.UpdateMocRecordReq;
import com.liuxinlong.modules.moc.service.MocRecordService;
import com.liuxinlong.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料变更记录信息控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2021-01-17
 */
@RestController
@RequestMapping("/moc/record")
public class MocRecordController {
    @Autowired
    private MocRecordService mocRecordService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询物料变更记录信息
     *
     * @param req 请求信息
     * @return 物料变更记录信息列表
     */
    @PostMapping("page")
//	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.MOC_RECORD,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageRecordInfo(@RequestBody QueryMocRecordReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("changeType", req.getChangeType());
        queryMap.put("ecnNo", req.getEcnNo());
        queryMap.put("keyword", req.getMaterial());
        queryMap.put("enterUser", req.getEnterUser());
        List<Map<String, Object>> dataList = mocRecordService.pageRecordInfo(queryMap);
        int totalNum = mocRecordService.getRecordCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询物料变更记录信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增物料变更记录信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增物料变更记录信息", business = LogBusinessConstants.MOC_RECORD, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addRecordInfo(@RequestBody UpdateMocRecordReq req) {
        MocRecord moc = new MocRecord();
        moc.setTitle(req.getTitle());
        moc.setEcnNo(req.getEcnNo());
        moc.setChangeType(req.getChangeType());
        moc.setReason(req.getReason());
        moc.setAttach(req.getAttach());
        moc.setChangeTime(req.getChangeTime());
        moc.setRemark(req.getRemark());
        mocRecordService.addRecordInfo(moc);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增物料变更记录信息成功");
        return resp;

    }

    /**
     * 修改物料变更记录信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改物料变更记录信息", business = LogBusinessConstants.MOC_RECORD, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateRecordInfo(@RequestBody UpdateMocRecordReq req) {
        MocRecord moc = new MocRecord();
        moc.setId(req.getId());
        moc.setTitle(req.getTitle());
        moc.setEcnNo(req.getEcnNo());
        moc.setChangeType(req.getChangeType());
        moc.setReason(req.getReason());
        moc.setAttach(req.getAttach());
        moc.setChangeTime(req.getChangeTime());
        moc.setRemark(req.getRemark());
        mocRecordService.updateRecordInfo(moc);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改物料变更记录信息成功");
        return resp;
    }

    /**
     * 删除物料变更记录信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除物料变更记录信息", business = LogBusinessConstants.MOC_RECORD, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteRecordInfo(@RequestBody BaseDeleteReq req) {
        mocRecordService.deleteRecordInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除物料变更记录信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入物料变更记录数据", business = LogBusinessConstants.MOC_RECORD, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importData(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = mocRecordService.importData((String) map.get("fileName"), Integer.parseInt((String) map.get("mocType")), (String) map.get("approve"), ObjectUtils.isEmpty(map.get("remark")) ? "" :(String)map.get("remark") );
        return resp;
    }

    /**
     * 导入变更记录信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入物料变更记录数据", business = LogBusinessConstants.MOC_RECORD, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddData(@RequestBody ImportMocDataReq req) {
        return mocRecordService.batchAddData(req.getDataList(), req.getRecordId());
    }
}
